package com.show.web.unit;

import com.show.common.Result;
import com.show.dto.unit.Unit;
import com.show.service.unit.UnitService;
import com.show.util.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 部件展示控制器
 *
 * @author liukai
 */
@RestController
@RequestMapping(value = "unit")
public class UnitShowDataController {


    private UnitService unitService;

    @Autowired
    public UnitShowDataController(UnitService unitService) {
        this.unitService = unitService;
    }


    /**
     * 根据部件id获取经纬度用于地图展示
     *
     * @param unitId 部件id
     * @author liukai
     */
    @PostMapping(value = "getUnit")
    public Result getUnit(String unitId) {
        try {
            Unit unit = unitService.findOne(unitId);
            if (unit == null) {
                return ResultUtil.error("未查询到部件相关信息!");
            }
            return ResultUtil.success("查询成功!", unit);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.error("后台程序出现异常! 查询部件信息失败");
        }
    }


    /**
     * 获取部件相关信息
     *
     * @param smId 地图里面的id
     * @return Result
     */
    @PostMapping(value = "getUnitBySmId")
    public Result getUnitBySmId(String smId) {
        try {
            Unit unit = unitService.getUnitBySmId(smId);
            return ResultUtil.success("查询成功!", unit);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.error("后台程序出现异常! 查询部件信息失败");
        }
    }


    /**
     * 查询所有报警的井盖
     *
     * @return Result
     */
    @PostMapping(value = "queryErrorUnitList")
    public Result queryErrorUnitList() {
        try {
            List<Unit> errorUnitList = unitService.queryErrorUnitList();
            return ResultUtil.success("查询成功!", errorUnitList);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.error("后台程序出现异常! 查询所有报警的井盖失败");
        }
    }

}
